package com.to.aboomy.bannersample.slice;

import com.to.aboomy.bannersample.ResourceTable;
import com.to.aboomy.bannersample.component.BannerAndList;
import com.to.aboomy.bannersample.component.ItemPagerNested;
import com.to.aboomy.bannersample.fragment.Test1Fragment;
import com.to.aboomy.bannersample.util.MyUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * NestedPagerAbilitySlice ViewPage嵌套
 */
public class NestedPagerAbilitySlice extends AbilitySlice {
    final List<Color> integers = new ArrayList<>();
    PageSlider viewPager;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_nested_pager);

        integers.add(Color.RED);
        integers.add(Color.GREEN);
        integers.add(Color.BLUE);
        integers.add(Color.YELLOW);

        viewPager = (PageSlider) findComponentById(ResourceTable.Id_viewPager);
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(integers);
        viewPager.setProvider(myPagerAdapter);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    private class MyPagerAdapter extends PageSliderProvider {
        List<Color> mColor;

        private MyPagerAdapter(List<Color> integers) {
            this.mColor = integers;
        }

        @Override
        public int getCount() {
            if (mColor == null) {
                return 0;
            } else {
                return mColor.size();
            }
        }

        @Override
        public Object createPageInContainer(ComponentContainer componentContainer, int i) {
            ItemPagerNested itemPagerNested = new ItemPagerNested(getAbility());

            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(RgbColor.fromArgbInt(mColor.get(i).getValue()));
            itemPagerNested.setBackground(shapeElement);

            itemPagerNested.setLayoutConfig(new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,DirectionalLayout.LayoutConfig.MATCH_PARENT));
            componentContainer.addComponent(itemPagerNested);
            return componentContainer;
        }

        @Override
        public void destroyPageFromContainer(ComponentContainer componentContainer, int i, Object o) {
            componentContainer.removeAllComponents();
        }

        @Override
        public boolean isPageMatchToObject(Component component, Object o) {
            return false;
        }
    }
}
